package com.trainfinder.database;

import java.text.SimpleDateFormat;

import com.trainfinder.requestor.IJourney;
import com.trainfinder.requestor.Requestor;

public class JourneyDetailsImpl implements JourneyDetails {

	private IJourney journey;

	private String percentageOfMaxPrice;

	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
			"dd/MM/yy h:mma");

	private static final SimpleDateFormat EUROSTAR_BOOKING_DATE_FORMAT = new SimpleDateFormat(
			"dd'%2F'MM'%2F'yyyy");

	private static final SimpleDateFormat EUROSTAR_BOOKING_TIME_FORMAT = new SimpleDateFormat(
			"HH'%3A'mm");

	private static final SimpleDateFormat HOUR_FORMAT = new SimpleDateFormat("H");

	public JourneyDetailsImpl(IJourney journey, int maxPriceInPence) {
		this.journey = journey;
		this.percentageOfMaxPrice = getPercentageOfMaxPrice(maxPriceInPence);
	}

	private static final long serialVersionUID = 1L;

	public String getHour() {
		return HOUR_FORMAT.format(journey.getId().getDate());
	}

	public String getPercentageOfMaxPrice() {
		return percentageOfMaxPrice;
	}

	public String getTravelDetailsDate() {
		// for example "08%2F06%2F2006";
		return EUROSTAR_BOOKING_DATE_FORMAT.format(journey.getId().getDate());
	}

	public String getTravelDetailsTime() {
		// eg "05%3A00";
		return EUROSTAR_BOOKING_TIME_FORMAT.format(journey.getId().getDate());
	}

	public String getdepartureCityCode() {
		String code = Requestor.getStationCode(journey.getFromStation());
		return code;
	}

	public String getArrivalCityCode() {
		String code = Requestor.getStationCode(journey.getToStation());
		return code;
	}

	private String getPercentageOfMaxPrice(int maxPrice) {

		int weekday = journey.getWeekdayPence();
		int weekend = journey.getWeekendPence();

		if (weekend == 0 && weekday == 0) {
			return "0";
		} else if (weekend == 0) {
			return Integer
					.toString((int) (100.00 * journey.getWeekdayPence() / maxPrice));
		} else if (weekday == 0) {
			return Integer
					.toString((int) (100.00 * journey.getWeekendPence() / maxPrice));

		} else if (weekend > 0 && weekday > 0) {

			if (weekday < weekend) {
				return Integer.toString((int) (100.00 * journey
						.getWeekdayPence() / maxPrice));
			} else {
				return Integer.toString((int) (100.00 * journey
						.getWeekendPence() / maxPrice));
			}
		}
		return "0";
	}

	public String getText() {
		String date1 = DATE_FORMAT.format(journey.getLastUpdatedWeekdayPrice());
		String date2 = DATE_FORMAT.format(journey.getLastUpdatedWeekendPrice());
		return "updated on "+date1+" and "+date2;
	}

	public String getWeekdayPrice() {

		double pence = journey.getWeekdayPence();
		int pounds = (int) (pence / 100);

		if (pence == 0)
			return "";
		else
			return Integer.toString(pounds) + "."
					+ doubleFigures(((int) pence - pounds * 100));
	}

	public String getWeekendPrice() {

		double pence = journey.getWeekendPence();
		int pounds = (int) (pence / 100);

		if (pence == 0)
			return "SoldOut";
		else
			return Integer.toString(pounds) + "."
					+ doubleFigures(((int) pence - pounds * 100));
	}

	private int getDepartureMin() {
		return journey.getDepartureMin();
	}

	private int getDepartureHour() {
		return journey.getDepartureHour();
	}

	private int getArivalMin() {
		return journey.getArivalMin();
	}

	private int getArivalHour() {
		return journey.getArivalHour();
	}

	public String doubleFigures(int i) {
		String str = Integer.toString(i);
		if (str.length() == 1) {
			return "0" + str;
		} else {
			return str;
		}
	}

	public String getTime() {
		return "" + doubleFigures(getDepartureHour()) + ":"
				+ doubleFigures(getDepartureMin()) + "-"
				+ doubleFigures(getArivalHour()) + ":"
				+ doubleFigures(getArivalMin());
	}

	public String getColour() {
		return "#ffea42";
	}

}
